{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 07\n",
    "# 列表生成式\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# 使用列表生成式，获得1 ~ 10之间所有偶数列表\n",
    "even_numbers = [num for num in range(1, 11)\n",
    "                if num % 2 == 0]  # 一行放不下\n",
    "\n",
    "print(even_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T10:51:13.845543Z",
     "end_time": "2024-07-03T10:51:13.852202Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [2, 4, 6], [3, 6, 9]]\n"
     ]
    }
   ],
   "source": [
    "# 嵌套列表生成式\n",
    "matrix = [[i * j for j in range(1, 4)]\n",
    "          for i in range(1, 4)]\n",
    "print(matrix)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T10:51:26.254529Z",
     "end_time": "2024-07-03T10:51:26.263072Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
